package com.example.moreeyes;

import android.util.Log;

import java.util.concurrent.TimeUnit;

import kotlin.jvm.Synchronized;
import okhttp3.ConnectionPool;
import okhttp3.Dispatcher;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OKHttpManager {
    private static final String TAG = "OKHttpManager";
    private static final MediaType JSON_MEDIA_TYPE = MediaType.parse("application/json;charset=utf-8");
    private static final int MAX_REQUESTS = 4;
    private static final int CONNECTION_POOL_SIZE = 10;
    private static final int CONNECTION_POOL_KEEP_ALIVE_DURATION = 5;
    private static final int TIMEOUT_DURATION = 5;
    private static volatile OKHttpManager instance;
    private final OkHttpClient httpClient;

    public static OKHttpManager getInstance(){
        if (instance == null){
            synchronized (OKHttpManager.class){
                if (instance == null){
                    instance = new OKHttpManager();
                }
            }
        }
        return instance;
    }

    private OKHttpManager(){
        this.httpClient = createHttpClient();
    }
    private static OkHttpClient createHttpClient(){
        Dispatcher dispatcher = new Dispatcher();
        dispatcher.setMaxRequests(MAX_REQUESTS);
        ConnectionPool connectionPool = new ConnectionPool(CONNECTION_POOL_SIZE,
                CONNECTION_POOL_KEEP_ALIVE_DURATION,
                TimeUnit.MINUTES);
        return  new OkHttpClient.Builder()
                .dispatcher(dispatcher)
                .connectionPool(connectionPool)
                .connectTimeout(TIMEOUT_DURATION,TimeUnit.SECONDS)
                .writeTimeout(TIMEOUT_DURATION,TimeUnit.SECONDS)
                .readTimeout(TIMEOUT_DURATION,TimeUnit.SECONDS)
                .build();
    }

    /**
     * 发送GET请求
     * @param url
     */
    public void sendGetRequest(String url){
        Request request = new Request.Builder().url(url).get().build();
        try(Response response = httpClient.newCall(request).execute()) {
            if (response.isSuccessful()){
                String responseStr = response.body().string();
                Log.d(TAG,"Response:" + responseStr);
            }else {
                Log.d(TAG,"Response:" + response.code());
            }
        }catch (Exception exception){
            Log.e(TAG,"exception:" + exception.toString());
        }
    }

    public String sendPostRequest(String url,String json){
        RequestBody requestBody = RequestBody.create(JSON_MEDIA_TYPE,json);
        Request request = new Request.Builder().url(url).post(requestBody).build();
        try (Response response = httpClient.newCall(request).execute()){
            if (response.isSuccessful()){
                String responseStr = response.body().string();
                Log.d(TAG,"Response:" + responseStr);
                return responseStr;
            }else {
                Log.d(TAG,"Response:" + response.code());
                return String.valueOf(response.code());
            }
        }catch (Exception exception){
            Log.e(TAG,"exception:" + exception.toString());
            return exception.toString();
        }
    }
}
